#include "pthread.h"
#include <semaphore.h>
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <fcntl.h>
#include <cstdio>
#include "producerConsumer.h"
#include <head.h>
using namespace std;
sem_t usedBytes,freeBytes;
#define STR "HELLOWORLD"
pthread_t pid1 ,pid2;
char c=0;
void semINIT()
{
    sem_init(&freeBytes,0,1);
    sem_init(&usedBytes,0,0);
}
void *producer(void *p)
{
    while(1){
        for(int i=0;i<10;i++){
            sem_wait(&freeBytes);
            c=STR[i];
            DEBUG_MSG("produce a data!\n");
            sem_post(&usedBytes);

        }
        break;
    }
    return NULL;
}
void *consumer(void *p)
{
    while(1){
        for(int i=0;i<10;i++){
            sem_wait(&usedBytes);
            c=STR[i];
            DEBUG_MSG("get a data[%d]=%c\n",i,c);
            sem_post(&freeBytes);

        }
        break;
    }
    return NULL;
}
void producerCOnsumer()
{
    semINIT();
    pthread_create(&pid1,NULL,producer,NULL);
    pthread_create(&pid1,NULL,consumer,NULL);
    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);
}